package com.openarena.persistence;

import com.openarena.model.*;
import com.openarena.statistics.Statistics;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    private static final Logger logger = Logger.getLogger(HibernateUtil.class.getCanonicalName());

    static {
        try {
            AnnotationConfiguration ac = new AnnotationConfiguration().configure();
            ac.addAnnotatedClass(Game.class).addAnnotatedClass(GameStat.class);
            ac.addAnnotatedClass(GameFlagEvent.class).addAnnotatedClass(GameKill.class);
            ac.addAnnotatedClass(GameParameter.class).addAnnotatedClass(GamePoint.class);
            ac.addAnnotatedClass(GameTeam.class).addAnnotatedClass(Player.class);
            ac.addAnnotatedClass(PlayerNick.class).addAnnotatedClass(Weapon.class);
            ac.addAnnotatedClass(WeaponNick.class).addAnnotatedClass(GameComment.class);

            sessionFactory = ac.buildSessionFactory();
        } catch (Throwable ex) {
            logger.log(Level.SEVERE, "Initial SessionFactory creation failed.", ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    private static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static Session getSession() {
        return getSessionFactory().openSession();
    }
}
